﻿using iTool_Cetector.Result;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

namespace iTool_Cetector.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class DockerController : ControllerBase
    {

        readonly GlobalStatus globalStatus;
        public DockerController(GlobalStatus globalStatus)
        {
            this.globalStatus = globalStatus;
        }

        [HttpPost("{container}")]
        public LinuxProcessStatus Status(string container)
        {
            try
            {
                var print = Utils.Excuter($"docker stats --no-stream {container}");
                if (string.IsNullOrWhiteSpace(print))
                {
                    return default;
                }

                var filter1 = print.Split($"docker stats --no-stream {container}&exit");
                var result = filter1[^1];

                if (string.IsNullOrWhiteSpace(result))
                {
                    return default;
                }

                result = result.TrimStart('\r').TrimStart('\n');
                result = result.TrimEnd('\r').TrimEnd('\n');

                var lines = result.Split("\n");
                var lastLine = lines[^1];
                var array = lastLine.Split(' ').Where(item => !string.IsNullOrWhiteSpace(item)).ToList();

                var lastResult = new List<string>();

                for (int i = 0; i < array.Count(); i++)
                {
                    string item = array[i];

                    if ((i+1) == array.Count())
                    {
                        lastResult.Add(item);
                    }
                    else
                    {
                        if (array[i+1] == "/")
                        {
                            lastResult.Add(item+ array[i + 1]+ array[i + 2]);
                            i++;
                            i++;
                        }
                        else
                        {
                            lastResult.Add(item);
                        }
                    }
                }

                //var model = new DockerContainerStatus();
                var memArr = lastResult[3].Split("/");
                //model.CPU = lastResult[2];
                //model.MEMUSAGE = memArr[0];
                //model.MEMLIMIT = memArr[1];
                //model.MEM = lastResult[4];
                //model.NETWORK = lastResult[5];
                //model.BLOCKIO = lastResult[6];

                LinuxProcessStatus model = new LinuxProcessStatus 
                {
                    IsRunInDocker = true,
                    CPU = memArr[0] == "0B" ? -1 : 0,
                    CPUString = lastResult[2],
                    MEM = 0,
                    MEMString = memArr[0]
                };

                if (model.CPU == -1)
                {
                    this.globalStatus.Instances = this.globalStatus.Instances.Where(item => item.Title != container).ToList();
                }

                return model;
            }
            catch (Exception ex)
            {
                return new LinuxProcessStatus
                {
                    CPUString = ex.Message
                };
            }
        }
    }



    
}
